import Taro, {Component} from '@tarojs/taro'
import { AtInput, AtForm ,AtButton,AtMessage} from 'taro-ui'
import {View} from "@tarojs/components"
import {showErrorMsg} from '../../../utils/index'
import {getGlobalData} from "../../../global.d";
import {createNewClass } from './service'
import './index.scss'

export default class Index extends Component {
  config = {
    navigationBarTitleText: '创建班级'
  };
    constructor(props) {
        super(props);
        this.state = {
          school:'',
          college: '',
          grade: '',
          majorOrClassName: '',
          classNumber:'',
          classKey:''
        }
    }

  handleChange(key, val) {
    this.setState({
      [key]: val
    });
  }
  doSubmit(){
    const {school, college, grade, majorOrClassName, classNumber, classKey} = this.state;
    if(school===''){
      showErrorMsg('请填写学校名称')
      return ;
    }
    if(majorOrClassName===''){
      showErrorMsg('请填写所在专业或课程名称')
      return ;
    }
    if(classNumber===''){
      showErrorMsg('请填写班级号')
      return ;
    }
    if(classKey===''){
      showErrorMsg('请填写班级密钥')
      return ;
    }
    const user = getGlobalData('user');
    createNewClass(user._id,user.userClass,{school, college, grade, majorOrClassName, classNumber, classKey})
  }
    render() {
      const {school, college, grade, majorOrClassName, classNumber, classKey} = this.state;
        return (
            <View>
              <AtForm
              >
                <AtInput
                  name='school'
                  title='学校'
                  type='text'
                  placeholder='*必填（如：东华大学）'
                  value={school}
                  onChange={this.handleChange.bind(this,'school')}
                />
                <AtInput
                name='college'
                title='学院'
                type='text'
                placeholder='如：计算机学院'
                value={college}
                onChange={this.handleChange.bind(this,'college')}
              />
                <AtInput
                  name='grade'
                  title='年级'
                  type='text'
                  placeholder='如：2018级'
                  value={grade}
                  onChange={this.handleChange.bind(this,'grade')}
                />
                <AtInput
                  name='majorOrClassName'
                  title='所在专业或课程名称'
                  type='text'
                  placeholder='*必填(如：网络工程||中国近代史)'
                  value={majorOrClassName}
                  onChange={this.handleChange.bind(this,'majorOrClassName')}
                />
                <AtInput
                  name='classNumber'
                  title='班级号'
                  type='text'
                  placeholder='*必填（如：1班）'
                  value={classNumber}
                  onChange={this.handleChange.bind(this,'classNumber')}
                />
                <AtInput
                  name='classKey'
                  title='班级密钥'
                  type='text'
                  placeholder='*必填（同学加入时需要,一旦设置无法更改）'
                  value={classKey}
                  onChange={this.handleChange.bind(this,'classKey')}
                />
                <AtButton type='primary' customStyle={{marginTop: '15PX'}}
                          onClick={this.doSubmit.bind(this)}
                >确认</AtButton>
              </AtForm>
              <AtMessage />
            </View>
        )
    }

}
